class Solution {
public:
    const static int N = 1024;
    int minChanges(vector<int>& nums, int k) {
        int n = nums.size();
        
        vector<int> group(k);
        vector<unordered_map<int,int>> group_num(k);
        for(int i = 0;i < n;i++){
            int g = i % k;
            group[g]++;
            group_num[g][nums[i]]++;
        }
        
        vector<vector<int>> dp(k,vector<int>(N));
        for(int j = 0;j < N;j++){
            dp[0][j] = group[0] - group_num[0][j];
        }
        
        for(int i = 1;i < k;i++){
            int Max = *min_element(dp[i-1].begin(),dp[i-1].end())+group[i];
            
            fill(dp[i].begin(),dp[i].end(),Max);
            
            for(auto& [num,cnt] : group_num[i]){
                for(int j = 0;j < N;j++){
                    dp[i][j^num] = min(dp[i][j^num],dp[i-1][j] + group[i] - cnt );
                }
            }
        }
        return dp[k-1][0];
    }
};
